//
//  BHMePersonalHeaderView.swift
//  BigHealth
//
//  Created by 蔺百好 on 2021/7/19.
//

import UIKit
protocol BHMePersonalHeaderViewDelegate {
    func didPersonalHomeChangeIndex(type: Int)
}

class BHMePersonalHeaderView: UIView {
    var delegate: BHMePersonalHeaderViewDelegate?
    
    
    @IBOutlet weak var iconImageView: UIImageView!
    
    @IBOutlet weak var name_label: UILabel!
    
    @IBOutlet weak var messageBtn: UIButton!
    
    @IBOutlet weak var attensionBtn: UIButton!
    
    @IBOutlet weak var bottom_view: UIView!
    
    @IBOutlet weak var move_line: UILabel!
    @IBOutlet weak var dynBtn: UIButton!
    
    @IBOutlet weak var likeBtn: UIButton!
    
    @IBOutlet weak var fans_view: UIView!
    @IBOutlet weak var attensionView: UIView!
    @IBOutlet weak var famousView: UIView!
    
    @IBOutlet weak var fans_label: UILabel!
    @IBOutlet weak var attension_label: UILabel!
    @IBOutlet weak var famous_label: UILabel!
    
    // -35  35
    @IBOutlet weak var move_lineCenterX: NSLayoutConstraint!
    static func loadFromXib() -> BHMePersonalHeaderView {
        let viewArray = Bundle.main.loadNibNamed("BHMePersonalHeaderView", owner: nil, options: nil)
        let view: BHMePersonalHeaderView = viewArray![0] as! BHMePersonalHeaderView
        return view
    }

    
    override func awakeFromNib() {
        self.attensionBtn.addLayerCorner(12, 0, .color_FC352B)
        self.iconImageView.addLayerCorner(23, 0, .color_FC352B)
        self.move_line.addLayerCorner(2, 0, .color_FC352B)

    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        if self.bottom_view.haveRaiuds == false {
            self.bottom_view.addCorner(conrners: [.topLeft,.topRight], radius: 12)
            self.bottom_view.haveRaiuds = true
        }
    }
    
    @IBAction func didChangeTypeAct(_ sender: UIButton) {
        if sender.isSelected == true {
            return
        }
        
        sender.isSelected = true
        if sender == self.dynBtn {
            self.likeBtn.isSelected = false
            self.move_lineCenterX.constant = -35
            self.delegate?.didPersonalHomeChangeIndex(type: 0)
        }else {
            self.dynBtn.isSelected = false
            self.move_lineCenterX.constant = 35
            self.delegate?.didPersonalHomeChangeIndex(type: 1)
        }
    }
    
    // 自己的头部刷新
    func refreshUserForSelf()  {
        let model = BHMeUserInfoModel.shared
        
        self.iconImageView.sd_setImage(with: URL.init(string: model.avatar), completed: nil)
        self.name_label.text = model.username
        self.fans_label.text = "\(model.fansNum)"
        self.attension_label.text = "\(model.attentionNum)"
        self.famous_label.text = "\(model.totalNum)"
        self.attensionBtn.isHidden = true
        self.messageBtn.isHidden = true
    }
    
    // 他人的头部刷新
    func refreshOther(model: BHUserPageDataModel)  {
        self.iconImageView.sd_setImage(with: URL.init(string: model.avatar), completed: nil)
        self.name_label.text = model.name
        self.fans_label.text = "\(model.fansNum)"
        self.attension_label.text = "\(model.attentionNum)"
        self.famous_label.text = "\(model.totalNum)"
        self.attensionBtn.isHidden = false
        self.messageBtn.isHidden = false
        self.attensionBtn.isSelected = model.attentionFlag == 1
    }
    
    
    
    static var height: CGFloat {
        get {
            let last = CGFloat(241-64)
            return UIScreen.begainY+last
        }
    }
    
    static var maxUp: CGFloat {
        return BHMePersonalHeaderView.height-50-UIScreen.begainY
    }
}
